package com.example.administrator.yuejian_15.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian_15.R;
import com.example.administrator.yuejian_15.bean.LoginBean;
import com.example.administrator.yuejian_15.listener.TListener;
import com.example.administrator.yuejian_15.model.LoginModel;


public class LoginActivity extends BaseActivity implements View.OnClickListener {
    EditText lg_phone,lg_password;
    TextView lg_wjmm,lg_qtfs,lg_regist;
    Button lg_login;
    CheckBox lg_jzmm;

    String uid;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_login);
        lg_phone = (EditText)findViewById(R.id.lg_phone);
        lg_password = (EditText)findViewById(R.id.lg_password);

        lg_wjmm = (TextView)findViewById(R.id.lg_wjmm);
        lg_qtfs =(TextView)findViewById(R.id.lg_qtfs);
        lg_regist = (TextView)findViewById(R.id.lg_regist);

        lg_login = (Button)findViewById(R.id.lg_login);

        lg_jzmm = (CheckBox)findViewById(R.id.lg_jzmm);

        boolean jzmmflag = getUer_remember();
        if (jzmmflag){
            String usernames = getuser();
            String passwords = getpass();
            lg_phone.setText(usernames);
            lg_password.setText(passwords);
            lg_jzmm.setChecked(jzmmflag);
        }
    }

    @Override
    void initEvent() {
        lg_login.setOnClickListener(this);
        lg_regist.setOnClickListener(this);

    }

    @Override
    void initDate() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.lg_login://监听登录按钮
                String username = lg_phone.getText().toString().trim();//取账号
                String password = lg_password.getText().toString().trim();//取密码
                if(username.equals("")||password.equals("")){
                    Toast.makeText(LoginActivity.this,"账号或密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else {
                    LoginModel loginModel=new LoginModel();
                    loginModel.login(username,password,loginBeanTListener);
                }
                break;
            case R.id.lg_regist://注册监听
                Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
                startActivityForResult(intent,1);
                break;

        }

    }
    TListener<LoginBean> loginBeanTListener = new TListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean loginBean) {
            String success=loginBean.getSuccess();//账号状态位
             uid = loginBean.getUid();//用户id
            String yhflag = loginBean.getYhflag();//冻结状态
            if (success.equals("0")){
                Toast.makeText(LoginActivity.this,"用户不存在",Toast.LENGTH_SHORT).show();
            }
            else if (success.equals("2")){
                Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
            }
            else if (success.equals("3")){
                Toast.makeText(LoginActivity.this,"账号已被冻结",Toast.LENGTH_SHORT).show();
            }
            else if (success.equals("1")){
                saveUserInfo();//保存用户信息
                Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                Intent intents = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intents);//跳转主页
            }
            else {
                Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginActivity.this,"获取失败，网络错误",Toast.LENGTH_SHORT).show();

        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if(resultCode == RESULT_OK){
                    String username = data.getStringExtra("username");
                    lg_phone.setText(username);
                    break;
                }
        }
    }

    private void saveUserInfo(){
        SharedPreferences.Editor editor = getSharedPreferences("userInfo",MODE_PRIVATE).edit();

        editor.putString("userid",uid);
        editor.putBoolean("remember",lg_jzmm.isChecked());
        if(lg_jzmm.isChecked()){
            editor.putString("username",lg_phone.getText().toString());
            editor.putString("userpass",lg_password.getText().toString());
        }
        editor.commit();
    }

}
